﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Routing;

namespace HU8.Helpers.Base
{
    /// <summary>
    /// site母板助理
    /// </summary>
    /// <author>李源锋</author>
    public class AdminSiteMenuHelper
    {
        /// <summary>
        /// 获取头部导航栏
        /// </summary>
        /// <returns></returns>
        public static string GetMenuTop(string providerName = "SiteMenu", object routeValues = null)
        {
            var provider = SiteMap.Providers[providerName];
            var rootNode = provider.RootNode;
            var result = new StringBuilder();
            var i = 0;
            foreach (SiteMapNode sNode in rootNode.ChildNodes)
            {
                if (i == 0)
                {
                    result.Append("<li class='navon'><em><a href='javascript:;' rel=" + i + " class='clickmenu'>");
                }
                else
                {
                    string url = getMenuItemUrl(sNode.Url, routeValues);
                    result.Append("<li><em><a href='" + url + "' rel=" + i + " class='clickmenu'>");
                }
                result.Append(sNode.Title);
                result.Append("</a></em></li>");
                i++;
            }
            return result.ToString();
        }
        /// <summary>
        /// 获取左侧导航栏的每一项
        /// </summary>
        /// <param name="node"></param>
        /// <param name="j"></param>
        /// <returns></returns>
        public static string GetMenuLeftItem(SiteMapNode node, int j, object routeValues = null)
        {
            if (node == null)
            {
                return string.Empty;
            }
            var subNode = new StringBuilder();
            if (node.HasChildNodes)
            {
                if (j == 0)
                {
                    subNode.Append("<ul id='menu" + j + "' style='display:block;'>");
                }
                else
                {
                    subNode.Append("<ul id='menu" + j + "' style='display:none;'>");
                }
                foreach (SiteMapNode sNode in node.ChildNodes)
                {
                    string url = getMenuItemUrl(sNode.Url, routeValues);
                    subNode.Append("<li><a class='' href='" + url + "' target='main'><em title='新窗口打开'></em>" + sNode.Title + "</a></li>");
                }
                subNode.Append("</ul>");
            }
            return subNode.ToString();
        }

        /// <summary>
        /// 获取左侧导航栏
        /// </summary>
        /// <returns></returns>
        public static string GetMenuLeft(string providerName = "SiteMenu", object routeValues = null)
        {
            var provider = SiteMap.Providers[providerName];
            var rootNode = provider.RootNode;
            var result = new StringBuilder();
            var j = 0;
            foreach (SiteMapNode sNode in rootNode.ChildNodes)
            {
                result.Append(GetMenuLeftItem(sNode, j, routeValues));
                j++;
            }
            return result.ToString();
        }

        private static string getMenuItemUrl(string nUrl, object routeValues)
        {
            string url = nUrl;
            if (string.IsNullOrEmpty(url))
            {
                url = "#";
            }
            else
            {
                if (routeValues != null)
                {
                    RouteValueDictionary dic = new RouteValueDictionary(routeValues);
                    string parametres = "";
                    foreach (KeyValuePair<string, object> p in dic.ToList())
                    {
                        parametres += p.Key + "=" + p.Value.ToString() + "&";
                    }
                    if (url.Contains("?"))
                    {
                        url = url + "&" + parametres;
                    }
                    else
                    {
                        url = url + "?" + parametres;
                    }
                }
            }
            return url;
        }
    }
}
